package org.hu.mapper;

import org.apache.ibatis.annotations.*;
import org.hu.entity.dto.OrderDetailAddDTO;
import org.hu.entity.dto.OrderDetailPageQueryDTO;
import org.hu.entity.dto.OrderPageQueryDTO;
import org.hu.entity.pojo.Order;
import org.hu.entity.pojo.OrderDetail;
import org.hu.entity.vo.DailyStats;
import org.hu.entity.vo.MonthlyStats;

import java.util.List;

@Mapper
public interface OrderMapper {
    // 添加订单细则(order_id的值是用订单返回的id)
    @Insert("insert into `order_detail`(dish_id,table_id,count,order_time,amount,order_id) " +
            "values(#{dishId},#{tableId},#{count},#{orderTime},#{amount},#{orderId})")
    void insertOrderDetail(OrderDetail orderDetail);

    // 删除订单细则
    @Delete("delete from `order_detail` where id=#{id}")
    void deleteOrderDetailById(Integer id);

    // 修改订单细则
    void updateOrderDetailById(OrderDetail orderDetail);

    // 条件查询订单细则
    List<OrderDetail> listOrderDetail(OrderDetailPageQueryDTO orderDetailPageQueryDTO);

    // 添加订单
    @Insert("insert into `order`(table_id,order_time,amount) " +
            "values (#{tableId},#{orderTime},#{amount})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "orderId")
    void insertOrder(OrderDetail orderDetail);

    // 查询订单根据状态和桌号
    @Select("SELECT * FROM `order` WHERE status IN (1, 2, 3, 4, 5) " +
            "AND table_id = #{tableId}")
    List<OrderDetail> selectOrderByStatusAndTableId(OrderDetailAddDTO orderDetailAddDTO);

    // 修改金额
    @Update("update `order` set amount=#{amount} + amount where id=#{orderId}")
    void updateOrderAmountById(OrderDetail orderDetail);

    // 条件查询订单
    List<Order> listOrder(OrderPageQueryDTO orderPageQueryDTO);

    // 修改订单
    void updateOrder(Order order);

    // 根据orderId查询订单详情
     @Select("SELECT * FROM `order_detail` WHERE order_id = #{id}")
    List<OrderDetail> selectOrderDetailById(Integer id);

      // 修改菜品月销量
    @Update("update dish set count=count+#{count} where id=#{dishId}")
    void updateDishMonthSaleById(Integer dishId, Integer count);

    // 将订单的状态修改为已支付(2)
    @Update( "update `order` set pay_status=2 where id=#{orderId}")
    void updatePayStatus(Integer orderId);

    // 获取某年每个月的销售数据
    List<MonthlyStats> getMonthlyStats(int year);

    // 获取某月每天的销售数据
    List<DailyStats> getDailyStats(int year, int month);

    // 用户订单列表
    @Select("SELECT * FROM `order_detail` WHERE status IN (1, 2, 3, 4, 5) " +
            "AND table_id = #{tableId}")
    List<OrderDetail> listUserOrderDetail(OrderDetailPageQueryDTO orderDetailPageQueryDTO);

    // 订单状态更新同步到订单细则状态
    @Update("update `order_detail` set status = #{status} where order_id = #{orderId}")
    void updateOrderDetailStatusByOrderId(Integer orderId, Integer status);
}
